#include "server.h"

#include <QTcpServer>
bool Server::init()
{
    auto tcpServer = new QTcpServer(&_httpServer);
    if (!tcpServer->listen(QHostAddress::Any, 8000)) {
        qWarning() << "TCP listen failed : " << tcpServer->errorString();
        return false;
    }

    if (!_httpServer.bind(tcpServer)) {
        qWarning() << "bind http server failed";
        return false;
    }

    // 配置路由
    _httpServer.route("/ping", []{
        qDebug() << "[http] 收到 ping 请求";
        return "pong";
    });

    _httpServer.route("/service/interview/search_interview", [=](const QHttpServerRequest& req) {
        return searchInterview(req);
    });

    _httpServer.route("/service/interview/search_question_cluster", [=](const QHttpServerRequest& req) {
        return searchQuestionCluster(req);
    });

    _httpServer.route("/service/interview/search_company", [=](const QHttpServerRequest& req) {
        return searchCompany(req);
    });

    _httpServer.route("/service/interview/upload_interview", [=](const QHttpServerRequest& req) {
        return uploadInterview(req);
    });

    return true;
}

Server::Server(QObject *parent)
    : QObject{parent}
{}

#include "interview.qpb.h"
#include "../Client/model/data.h"
QHttpServerResponse Server::searchInterview(const QHttpServerRequest &req)
{
    quan_mian::SearchInterviewReq pbReq;
    pbReq.deserialize(&_serializer, req.body());
    qDebug() << "[http] 收到 搜索面经 请求, reqId = " << pbReq.requestId();

    quan_mian::SearchInterviewRsp pbRsp;
    pbRsp.setRequestId(pbReq.requestId());
    pbRsp.setSuccess(true);
    pbRsp.setErrmsg("");

    QList<quan_mian::InterviewInfo> list;
    auto info = model::InterviewInfo();
    info.timestamp = time(0);
    info.sender.nickname = "测试";
    info.title = "测试文章";
    info.content = "这是测试文章的内容这是测试文章的内容.\n";
    QString addContent = "测试";
    for(int i = 0; i < 10; ++i) addContent = addContent + addContent;
    info.content += addContent;
    int n = std::rand() % 100;
    for(int i = 0; i < n; ++i) list.append(info);
    pbRsp.setInterviewList(list);

    QByteArray body = pbRsp.serialize(&_serializer);

    QHttpServerResponse rsp(body, QHttpServerResponse::StatusCode::Ok);
    QHttpHeaders headers;
    headers.append("Content-Type", "application/x-protobuf");
    rsp.setHeaders(headers);
    return rsp;
}

QHttpServerResponse Server::searchQuestionCluster(const QHttpServerRequest &req)
{
    quan_mian::SearchQuestionClusterReq pbReq;
    pbReq.deserialize(&_serializer, req.body());
    qDebug() << "[http] 收到 搜索题簇 请求, reqId = " << pbReq.requestId();

    quan_mian::SearchQuestionClusterRsp pbRsp;
    pbRsp.setRequestId(pbReq.requestId());
    pbRsp.setSuccess(true);
    pbRsp.setErrmsg("");

    quan_mian::QuestionClusterInfo info;
    quan_mian::QuestionInfo question;
    question.setContent("这是一个问题");
    question.setTags(QList<QString>({"标签1", "标签2", "标签3", "标签4"}));
    info.setStandardAnswer("这是一个问题的答案");
    QList<quan_mian::QuestionClusterInfo> list;
    for(int i = 1, n = rand() % 100; i < n; ++i) {
        QList<quan_mian::QuestionInfo> questions;
        for(int j = 0; j < i; ++j) questions.append(question);
        info.setQuestions(questions);
        info.setCnt(info.questions().size());
        info.setTags(QList<QString>({"标签1", "标签2", "标签3", "标签4"}));
        list.append(info);
    }
    pbRsp.setQuestionClusterList(list);

    QByteArray body = pbRsp.serialize(&_serializer);

    QHttpServerResponse rsp(body, QHttpServerResponse::StatusCode::Ok);
    QHttpHeaders headers;
    headers.append("Content-Type", "application/x-protobuf");
    rsp.setHeaders(headers);
    return rsp;
}

QHttpServerResponse Server::searchCompany(const QHttpServerRequest &req)
{
    quan_mian::SearchCompanyReq pbReq;
    pbReq.deserialize(&_serializer, req.body());
    qDebug() << "[http] 收到 搜索公司 请求, reqId = " << pbReq.requestId();

    quan_mian::SearchCompanyRsp pbRsp;
    pbRsp.setRequestId(pbReq.requestId());
    pbRsp.setSuccess(true);
    pbRsp.setErrmsg("");

    quan_mian::CompanyInfo info;
    info.setCompanyName("公司名");
    info.setWindowCnt(12);
    info.setTimestamp(time(0));
    info.setCi(12);

    info.setTopTags({"tag1", "tag2", "tag3"});
    info.setTopTagsPct({96, 66, 36});
    info.setUpTags({"tag4", "tag5", "tag6"});
    info.setUpTagsPct({96, 66, 36});

    QList<quan_mian::CompanyInfo> list;
    for(int i = 0, n = rand() % 100; i < n; ++i) list.append(info);
    pbRsp.setCompanyList(list);

    QByteArray body = pbRsp.serialize(&_serializer);

    QHttpServerResponse rsp(body, QHttpServerResponse::StatusCode::Ok);
    QHttpHeaders headers;
    headers.append("Content-Type", "application/x-protobuf");
    rsp.setHeaders(headers);
    return rsp;
}

QHttpServerResponse Server::uploadInterview(const QHttpServerRequest &req)
{
    quan_mian::UploadInterviewReq pbReq;
    pbReq.deserialize(&_serializer, req.body());
    qDebug() << "[http] 收到 上传面经 请求, reqId = " << pbReq.requestId() << " 标题 = " << pbReq.interview().title();

    quan_mian::UploadInterviewRsp pbRsp;
    pbRsp.setRequestId(pbReq.requestId());
    pbRsp.setSuccess(true);
    pbRsp.setErrmsg("");

    QByteArray body = pbRsp.serialize(&_serializer);

    QHttpServerResponse rsp(body, QHttpServerResponse::StatusCode::Ok);
    QHttpHeaders headers;
    headers.append("Content-Type", "application/x-protobuf");
    rsp.setHeaders(headers);
    return rsp;
}
